﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using System.Configuration;
using System.Collections;
using Cosmetic;
using Cosmetic.DataEntity;

namespace WebApp.Product
{
    public partial class ProductInfo_rh2 : PageBase
    {

        public float TotalTags { get; set; }
        public float TotalComments { get; set; }

        private ProductManager m_ProductManager = new ProductManager();
        private CommentManager m_CommentManager = new CommentManager();
        private TagManager m_TagManager = new TagManager();
        private UserManager m_UserManager = new UserManager();
        private CategoryManager m_CategoryManager = new CategoryManager();
        private SaleManager m_salemanager = new SaleManager();
        private PlateManager PM = new PlateManager();

        string NotifyAddress = ConfigurationManager.AppSettings["NotifyAddress"];
        string ReturnAddress = ConfigurationManager.AppSettings["ReturnAddress"];

        Cosmetic.DataEntity.Product m_Product;
        Cosmetic.DataEntity.Product m_ProductInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["PID"]))
            {
                string a = this.Request.QueryString["PID"];
                Session["pid"] = this.Request.QueryString["PID"];
                //this.m_Product = this.m_ProductManager.GetProductById1(new Guid(a));
                this.m_Product = this.m_ProductManager.GetProductByUID(a);
                this.m_ProductInfo = this.m_ProductManager.GetProductByUID(a);
                //this.m_ProductInfo = this.m_ProductManager.GetProductInfoById_Con(new Guid(a));
            }
            else
            {
                string b = Session["pid"].ToString();
                //this.m_Product = this.m_ProductManager.GetProductById1(new Guid(b));
                this.m_Product = this.m_ProductManager.GetProductByUID(b);
                this.m_ProductInfo = this.m_ProductManager.GetProductByUID(b);
                //this.m_ProductInfo = this.m_ProductManager.GetProductInfoById_Con(new Guid(b));
            }

            //this.HLThirdCategory.Text = m_Product.ProductCategory.Name;
            //this.HLThirdCategory.NavigateUrl = "~/Product/ProductCategory.aspx?CID=" + m_Product.ProductCategory.CategoryId;

            //this.HLThirdCategory.Text = m_Product.ProductCategory.SupCategory.Name;
            //this.HLThirdCategory.NavigateUrl = "~/Product/ProductCategory.aspx?CID=" + m_Product.ProductCategory.SupCategoryId + "&SID=" + m_Product.ProductCategory.CategoryId;


            string topCategoryId = m_Product.ProductCategory.SupCategory.SupCategory.CategoryId.ToString();
            string secondCategoryId = m_Product.ProductCategory.SupCategory.CategoryId.ToString();
            string CategoryId = m_Product.ProductCategory.CategoryId.ToString();

            string topCategoryName = m_Product.ProductCategory.SupCategory.SupCategory.Name.ToString();
            string secondCategoryName = m_Product.ProductCategory.SupCategory.Name.ToString();
            string CategoryName = m_Product.ProductCategory.Name.ToString();

            this.HLThirdCategory.Text = CategoryName;
            this.HLThirdCategory.NavigateUrl = "~/Product/ProductCategory.aspx?CID=" + secondCategoryId + "&SID=" + CategoryId;

            this.HLSecondCategory.Text = secondCategoryName;
            this.HLSecondCategory.NavigateUrl = "~/Product/ProductCategory.aspx?CID=" + secondCategoryId;

            this.HLTopCategory.Text = topCategoryName;
            this.HLTopCategory.NavigateUrl = "~/Product/ProductTopCategory.aspx?CID=" + topCategoryId;



            //this.Literal2.Text = m_Product.Name;
            this.img.Src = m_ProductInfo.RecommendPicture1;
            this.LtName.Text = m_Product.Name;
            this.Literal2.Text = m_ProductInfo.ProductCode;
            this.Literal1.Text = m_ProductInfo.Name;


            //this.Page.Title = string.Format("【{0}】价格,{1}什么牌子好_{1}哪里买?首选日花商城!", m_Product.Name, m_Product.ProductCategory.Name);
            //this.Page.MetaKeywords = string.Format("{0}价格,{0}评价,{1},日花商城", m_Product.Name, m_Product.ProductCategory.Name);
            //this.Page.MetaDescription = string.Format("{0}价格,{1}哪种好?{0}质量怎么样?日花商城全知道,网购{1},就上日花商城【直销,便捷,诚信！】", m_Product.Name, m_Product.ProductCategory.Name);

            this.Page.Title = string.Format("{0}【价格-参数-正品】", m_Product.Name);
            this.Page.MetaKeywords = string.Format("{0},{0}价格", m_Product.Name);
            this.Page.MetaDescription = string.Format("日花商城提供{0}的最新报价及使用说明， {1}", m_Product.Name, m_Product.Description);

            if (m_ProductInfo.eprice == Convert.ToDecimal(0.01))
            {
                ImageButton1.Visible = false;
                AskPrice.Visible = true;
                this.Literal4.Text = "询价";
                ImageButton2.Visible = false;
                
            }
            else
            {
                AskPrice.Visible = false;
                this.Literal4.Text = "￥" + Convert.ToString(m_ProductInfo.eprice);
            }
            this.Literal3.Text = Convert.ToString(m_ProductInfo.Price);
           // this.LiteralCommon2.Text = m_ProductInfo.Description;
            //this.LabelCountAll.Text = Convert.ToString(m_ProductInfo.网络零售价);
            // this.Literal6.Text = m_ProductInfo.Name;
           
            //this.LabelCountAll.Text = Convert.ToString(m_ProductInfo.网络零售价);
            // this.Literal6.Text = m_ProductInfo.Name;
        }

        //加入购物车
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Buy();
            if (Session["buycarts"] != null)
            {

                Page.RegisterStartupScript("openconfirm ", " <script> if(confirm( '尊敬的用户，由于您选择的产品属于不同\\n的支付系统，所以您的购物车内原有的商\\n品会自动取消,是否继续操作？ ')){self.location='../Shopping/shopCart.htm'} else{self.location='../Shopping/shopCarts.htm'} </script> ");

                return;
            }
            else
            {
                Response.Redirect("~/Shopping/shopCart.aspx");
            }
        }

        private void Buy()
        {
            try
            {
                int theCount = Convert.ToInt32(this.txtQuantity.Text.Trim());

                //add to buy cart
                if (null == Session["buycart"])
                {
                    Session["buycart"] = new Hashtable();
                }
                //加入尺寸
                if (null == Session["size"])
                {
                    Session["size"] = new Hashtable();
                }
                try
                {
                    if (((Hashtable)Session["buycart"]).Contains(m_Product.ProductId.ToString()))
                    {
                        int tint = int.Parse(((Hashtable)Session["buycart"])[m_Product.ProductId.ToString()].ToString());
                        ((Hashtable)Session["buycart"])[m_Product.ProductId.ToString()] = (++tint).ToString();

                    }
                    else
                    {
                        ((Hashtable)Session["buycart"]).Add(m_Product.ProductId.ToString(), theCount.ToString());

                    }
                    //Response.Redirect("~/Shopping/shopCart.htm");
                }
                catch (Exception)
                {
                    WebGeneric.WebMessageBox("购物篮添加失败!", this.Page);
                }

            }
            catch
            {
                WebGeneric.WebMessageBox("请正确填写购买数量!", this.Page);
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (null == this.SignInUser)
            {
                this.Response.Redirect(String.Format("~/Login.htm", this.Request.Url.PathAndQuery));
            }
            if (this.m_UserManager.GetFavoriteById(this.SignInUser.UserId, this.m_Product.ProductId) == null)
            {
                Favorite theFavorite = new Favorite();
                theFavorite.CreateTime = DateTime.Now;
                theFavorite.FavoriteId = Guid.NewGuid();
                theFavorite.ProductId = this.m_Product.ProductId;
                theFavorite.IsFavorite = true;
                theFavorite.UserId = this.SignInUser.UserId;

                this.m_UserManager.AddFavorite(theFavorite);
            }
            Page.RegisterStartupScript("openalert", "<script>alert('收藏成功！');</script>");
            return;
        }

        protected void btnSbmit_Click(object sender, ImageClickEventArgs e)
        {
            if (this.SignInUser == null)
            {
                Cosmetic.WebGeneric.WebMessageBox("您还未登陆！只有登陆了，才能发表评论。", this.Page);
                return;
            }
            //add comment
            Comment NewComment = new Comment();
            NewComment.CommentId = Guid.NewGuid();
            NewComment.ProductId = m_Product.ProductId; //new Guid(Request.QueryString["PID"]);
            NewComment.UserId = this.SignInUser.UserId;
            NewComment.Content = this.txtCommentContent.Text.Trim();
            NewComment.CreateTime = DateTime.Now;
            NewComment.BigCategory = m_CommentManager.GetProductsBigCategory(m_Product.ProductId);
            NewComment.PurchaseChannelsCategoryId = new Guid("759dc19a-c30f-4425-8160-205ea6020cbf");
            NewComment.IsSample = true;
            NewComment.Score = 5;
            //switch (RadioButtonList1.SelectedItem.Text)
            //{
            //    case "较差": { NewComment.Score = 1; }; break;
            //    case "一般": { NewComment.Score = 2; }; break;
            //    case "较好": { NewComment.Score = 3; }; break;
            //    case "很好": { NewComment.Score = 4; }; break;
            //    case "非常好": { NewComment.Score = 5; }; break;
            //    default: NewComment.Score = 5;
            //        break;
            //}
            NewComment.Tag = "";
            //NewComment.User = this.SignInUser;


            m_CommentManager.Insert(NewComment);
           // this.BindComment(this.m_ProductManager.GetProductById(m_Product.ProductId));

            this.txtCommentContent.Text = "";
            WebGeneric.WebMessageBox("添加成功!", this.Page);
            return;
            //Response.Redirect(Request.Url.ToString());
            //Server.Transfer(Request.Url.ToString());

            //WebGeneric.WebMessageBox("添加评论成功!", this.Page);
            //return;
        }

    }//end class
}